package com.mylib.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.mylib.libcore.R;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/20-9:38
 * @description  拖拽recycleView-item
 **/
public class DragRecycleView {

    private RecyclerView recycler;
    private OnItemDragListener onItemDragListener;
    private OnItemSwipeListener onItemSwipeListener;
    private ItemDragAndSwipeCallback itemDragAndSwipeCallback;
    private ItemTouchHelper itemTouchHelper;
    private String fixedText="";//固定不可移除的文字
    private ArrayList<String> lisTitle = new ArrayList<>();
    private int from =0;
    private int to =0;
    public ArrayList<String> getLisTitle() {
        return lisTitle;
    }

    public void setLisTitle(ArrayList<String> lisTitle) {
        this.lisTitle = lisTitle;
    }

    /**
     * 必须初始化
     */
    public void initCustomDragRecycler(RecyclerView recyclerView,BaseItemDraggableAdapter adapter,int spamCount,String fixedString) {
        if(!UIUtils.isEmpty(fixedString))this.fixedText = fixedString;
        recycler = recyclerView;
        RecyclerView.LayoutManager layoutManager =new StaggeredGridLayoutManager(spamCount, StaggeredGridLayoutManager.VERTICAL);
        recycler.setLayoutManager(layoutManager);
        onItemDragListener = new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
                //  ToastUtil.show("你在拖拽第"+"===>>"+pos);
                from =0;
                to =0;
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int fromIndex, RecyclerView.ViewHolder target, int toIndex) {
                from = fromIndex;
                to = toIndex;
            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
                // ToastUtil.show("你拖拽dao"+"===>>"+pos);
                 adapter.setNewData(lisTitle);
            }
        };

        recycler.setAdapter(adapter);
        itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(adapter);
        itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(recycler);
        // 开启拖拽
        adapter.enableDragItem(itemTouchHelper, R.id.ll_item, true);
        adapter.setOnItemDragListener(onItemDragListener);
        adapter.enableSwipeItem();
        adapter.setOnItemSwipeListener(onItemSwipeListener);

    }

    public void notifyDragRecycleViewData(ArrayList<String> lisTitle){
        Log.d("DragRecycleView", JsonUtil.toJson(lisTitle)+"=3334==");
        this.lisTitle = lisTitle;
    }




}
